package day28;

import java.util.*;


/**
 * 反转部分单项链表
 */

/**
 * 给定一个单链表，在链表中把第L个节点到第R个节点这一部分进行反转。
 */

class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int val) {
        this.val = val;
        this.next = null;
    }
}


public class partReserve {
    static Scanner scanner = new Scanner(System.in);
    /**
     * n表示单链表的长度。
     * val表示单链表各个节点的值。
     * 工表示翻转区间的左端点。
     * R表示翻转区间的右端点。
     * @param args
     */
    public static void main(String[] args) {
        int n = scanner.nextInt();
        ListNode head = create(n);
        int left = scanner.nextInt();
        int right = scanner.nextInt();
        head = partReserve(head, left, right);
        print(head);
    }

    private static ListNode create(int n) {
        ListNode head = new ListNode(0);
        ListNode cur = head;
        while (n-- > 0) {
            cur.next = new ListNode(scanner.nextInt());
            cur = cur.next;
        }
        return head.next;
    }

    private static void print(ListNode head) {
        while(head!=null){
            if (head.val != 0){
                System.out.print(head.val+" ");
                head=head.next;
            }
        }
    }

    public static ListNode partReserve(ListNode head,int left,int right){
        if (head == null || left == right){
            return head;
        }
        //start指向left结点的前驱结点
        ListNode ph = new ListNode(-1);
        ph.next = head;
        ListNode start = ph;
        int x = left;
        while (x-- > 1){
            start = start.next;
        }

        ListNode cur = start.next;
        ListNode phead = cur;
        //cur 指向第left个结点
        ListNode curnext = cur.next;

        while (curnext != null && left < right) {
            ListNode tmp = curnext.next;
            curnext.next = cur;
            cur = curnext;
            curnext = tmp;
            left++;
        }
        start.next = cur;
        phead.next = curnext;
        return ph.next;
    }
}